package com.jxinternet.platform.services.handler;

import com.google.protobuf.MessageLite;
import com.jxinternet.platform.services.util.CoderUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 编码器1
 * 编内层第一层
 */
@Sharable
public class CustomProtobufEncoder extends MessageToByteEncoder<MessageLite> {


    @Override
    protected void encode(
            ChannelHandlerContext ctx, MessageLite msg, ByteBuf out) throws Exception {

        byte[] body = msg.toByteArray();
        byte[] headerBody = CoderUtil.intToBytes(body.length);

        out.writeBytes(headerBody);
        out.writeBytes(body);

        return;
    }


}
